package com.woniu.adminservice.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Auther: 杨世民  <br/>
 * Date: 2020/3/25 : 9:07  <br/>
 * Description:  sql监控配置
 */
@Configuration
public class DruidConfig {

    //servlet  用来展示视图
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        //视图展示地址
        ServletRegistrationBean registrationBean= new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //视图登录账号
        Map account = new HashMap();
        account.put("loginUsername","admin");
        account.put("loginPassword","123");
        registrationBean.setInitParameters(account);
        return registrationBean;
    }

    //filter  用来收集数据
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //注册过滤器:WebStatFilter
        filterRegistrationBean.setFilter(new WebStatFilter());
        //对所有请求进行拦截
        filterRegistrationBean.addUrlPatterns("/*");
        Map resource = new HashMap();
        //排除在外的资源
        resource.put("exclusions","*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(resource);
        return filterRegistrationBean;
    }
}
